package web.tablas;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

public class TablaHome extends WebPage{
	private static final long serialVersionUID = 1L;
	private Component form;
	private Wasd nuevoWasd;
	private Proveedor proveedor;

	public TablaHome() {
		this.add(setForm(new Form<Void>("form").add(
			wasd_cosa(),
			wasd_otraCosa(),
			wasd_agregar(),
			tabla()
		)));
	}

	private Component tabla() {
		List<IColumn<Wasd>> columnas = new ArrayList<IColumn<Wasd>>();
		columnas.add(new PropertyColumn<Wasd>(new Model<String>("String"), "cosa"));
		columnas.add(new PropertyColumn<Wasd>(new Model<String>("String"), "otraCosa"));
		
		proveedor = new Proveedor();  
		return new DataTable<Wasd>("tabla", columnas, proveedor, 10);
	}

	private Component wasd_agregar() {
		return new AjaxButton("wasd_agregar") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				proveedor.agregar(nuevoWasd);
				nuevoWasd = new Wasd();
				target.add(form);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {}
		};
	}

	private Component wasd_otraCosa() {
		IModel<String> modelo = new PropertyModel<String>(this, "nuevoWasd.otraCosa");
		return new TextField<String>("wasd_otraCosa", modelo);
	}
	private Component wasd_cosa() {
		IModel<String> modelo = new PropertyModel<String>(this, "nuevoWasd.cosa");
		return new TextField<String>("wasd_cosa", modelo);
	}
	//getters&setters
	public void setNuevoWasd(Wasd nuevoWasd) {
		this.nuevoWasd = nuevoWasd;
	}
	public Wasd getNuevoWasd() {
		return nuevoWasd;
	}
	public Component setForm(Component form) {
		this.form = form;
		return form;
	}
	public Component getForm() {
		return form;
	}
	public void setProveedor(Proveedor proveedor) {
		this.proveedor = proveedor;
	}
	public Proveedor getProveedor() {
		return proveedor;
	}
}